package com.wind.framework.helper;

import com.wind.framework.annotation.Inject;
import com.wind.framework.util.ArrayUtil;
import com.wind.framework.util.CollectionUtil;
import com.wind.framework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author： BeiYan
 * @Date： 2016-11-27 13:37
 * @Description:依赖注入助手类
 */

public final class IocHelper {

    static {
        Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap();
        if (CollectionUtil.isNotEmpty(beanMap)) {
            for (Map.Entry<Class<?>, Object> entry : beanMap.entrySet()) {
                Class<?> beanClass = entry.getKey();
                Object beanInstance = entry.getValue();
                //获取所有field
                Field[] fields = beanClass.getFields();
                if (ArrayUtil.isNotEmpty(fields)) {
                    for (Field field : fields) {
                        if (field.isAnnotationPresent(Inject.class)) {

                            //在BeanMap中获取实例
                            Class<?> beanFieldClass = field.getType();
                            Object beanFieldInstance = beanMap.get(beanFieldClass);
                            if (beanFieldInstance != null) {
                                ReflectionUtil.setField(beanInstance, field, beanFieldInstance);
                            }
                        }
                    }
                }


            }


        }
    }
}

